
function FlickrPhoto(jsonPhoto, ajaxManager)
{
	var that_ = this; // required for private function support

	var jsonPhoto_ = jsonPhoto;
	var ajaxManager_ = ajaxManager;

	this.ORIGINAL = 0;
	this.NEW = 1;
	this.NEEDS_REMOVAL = -1;
	var tags_ = null;
	parseTagsString_(jsonPhoto_.tags);

	this.getId = function() { return jsonPhoto_.id; }
	this.getUrl = function(small) { return small ? jsonPhoto_.url_s : jsonPhoto_.url_m; }

	this.getTags = function() { return tags_; }

	this.addTag = function(tag)
	{
		var currentStatus = tags_[tag];
		if (currentStatus == undefined)
			tags_[tag] = this.NEW;
		else if (currentStatus == this.NEEDS_REMOVAL)
			tags_[tag] = this.ORIGINAL;
	}

	this.removeTag = function(tag)
	{
		var currentStatus = tags_[tag];
		if (currentStatus == this.NEW)
			delete tags_[tag];
		else if (currentStatus == this.ORIGINAL)
			tags_[tag] = this.NEEDS_REMOVAL;
	}

	this.applyTags = function()
	{
		var applyTags = false;
		var newTags = new Array();
		for (tag in tags_) {
			var status = tags_[tag];
			if (status != this.ORIGINAL)
				applyTags = true;
			if (status != this.NEEDS_REMOVAL)
				newTags[tag] = this.ORIGINAL; // remark status in new array because it will soon be applied
		}
		if (!applyTags)
			return;

		tags_ = newTags;

		var tagsToApply = "";
		for (tag in tags_)
			tagsToApply += tag + " ";
		// fixme: might need to take trailing space off tagsToApply

		var params = {
			"photo_id": jsonPhoto_.id,
			"tags" : tagsToApply
		};
		ajaxManager.post("flickr.photos.setTags", params);
	}


	function parseTagsString_(tagsString)
	{
		tags_ = new Array();
		var tempTags = tagsString.split(" ");
		for (i in tempTags)
			if (tempTags[i] != "")
				tags_[tempTags[i]] = that_.ORIGINAL;
	}


	// currently unused
	//function parseJsonTags_(jsonTags)
	//{
	//	tags_ = new Array();
	//	for (i in jsonTags)
	//		tags_[jsonTags[i]._content] = that_.ORIGINAL;
	//}
}

